package org.jcb.dao.data;

import org.jcb.model.ChineseQuestionnaire;
import org.jcb.model.Choix;
import org.jcb.model.User;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class GlobalDAO extends SQLiteOpenHelper {
public static final String DB_NAME = "db" ; 

public GlobalDAO(Context context) {
		super(context, DB_NAME, null, 1);
		
	}


public Choix[] getChoixPinyin(int id_type_quizz, User user) {
	Log.i("getChoixPinyin", "entree dans getChoixPinyin id_type_quizz" + id_type_quizz);
	Choix[] choixx = new Choix[10];
	SQLiteDatabase db = this.getWritableDatabase();
	Cursor cursor = db.rawQuery("select id_quizz,num_screen from quizz where id_type_quizz=" + id_type_quizz, null);
	int i = 0 ;
	while (cursor.moveToNext()) {
		int idQuizz = cursor.getInt(0) ;
		Log.i("getChoixPinyin", "id_quizz=" + idQuizz );
		choixx[i] = getChoix(idQuizz) ;
		choixx[i].setLibelle("0" + cursor.getInt(1) ) ;
		i++ ;
	}	
	Log.i("getChoixPinyin", "plus rien pour id_type_quizz=" + id_type_quizz );
	cursor.close();
	return choixx ;
}
	
	/*
	 * modifier :tenir compte user
	 */
	public Choix getChoix(int idQuizz) {
		Choix choix = new Choix();
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery("select id_quizz, note from quizz_result where id_quizz=" + idQuizz, null);
		Log.i("", "id_quizz=" + idQuizz );
		if (cursor.moveToNext()) {
			int note = cursor.getInt(1) ;
			Log.i("", "id_quizz=" + cursor.getInt(0) + " note=" + note);
			choix.setNbStars(note);
			choix.setActif(true);
		}
		else {
			choix.setNbStars(0);
			choix.setActif(false);
		}
		//choix.setLibelle("0" + String.valueOf(idQuizz)) ;
		cursor.close();
		return choix ;
	}
	
	public void deleteQuizzResult(User user) {
		Log.i("", "recordQuizzResultkkk");
		SQLiteDatabase db = getWritableDatabase();
		String[] args = {} ;
		int nbRows = db.delete("quizz_result", " id_quizz <> 1 " , args );
		Log.i("", "Nb rows deleted=" + nbRows);		
	}

	public int getIdQuizz(int idTypeQuizz, int NumQuizz) {
		return NumQuizz ; //TEMPORAIRE !!!! faire voir table quizz ou num et typequizz correspondent
	}

	public Integer getNextQuizz(int idTypeQuizz, int NumQuizz) {
		return new Integer(NumQuizz + 1); //TEMPORAIRE !!!! faire voir table quizz ou num et typequizz correspondent
	}	
	
	/*
	 * TODO Modifier
	 * en prametre on ne connaite que le numero et id_quizz 
	 * il faut chercher le quizz result de meme id et meme ordre de question
	 */	
	public void recordQuizzResult(ChineseQuestionnaire quest,User user) {
		Log.i("", "recordQuizzResultkkk");
		SQLiteDatabase db = getWritableDatabase();
		//String requete = "update  quizz_result set note = "+ note  + " where id_quizz=" + idQuizz ; 
		//db.rawQuery(requete, null);
		
		ContentValues cv = new ContentValues();
		cv.put("note", quest.getScore());			
		String[] args = { String.valueOf(quest.getIdQuizz()) } ;
		int nbRows = db.update("quizz_result",cv, " id_quizz=?, " , args );
		Log.i("", "Nb rows updated=" + nbRows);
		
		//controle
		getChoix(quest.getIdQuizz());
		
		
		Log.i("", "fait");
	}


	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		Log.i("onCreate", "recordQuizzResultkkk");
		db.execSQL("CREATE TABLE type_quizz (id_type_quizz INTEGER PRIMARY KEY, lib_type_quizz TEXT);");
		ContentValues cv = new ContentValues();
		cv.put("id_type_quizz", "1");
		cv.put("lib_type_quizz", "quizz pinyin");
		db.insert("type_quizz", "title", cv);			
		
		db.execSQL("CREATE TABLE quizz (id_quizz INTEGER PRIMARY KEY, id_type_quizz INTEGER, num_screen INTEGER);");
		cv = new ContentValues();
		for (int i = 1 ; i < 11 ; i++) {
			cv.put("id_quizz", i);
			cv.put("id_type_quizz", 1);
			cv.put("num_screen", i );
			db.insert("quizz", "title", cv);
			Log.i("onCreate", "insert quizz num=" + i );
		}
		//int dum = 0 ;
		
		db.execSQL("CREATE TABLE quizz_result (id_quizz INTEGER, user TEXT,  note INTEGER);");
		
		//inserer donnes intiales
		int i = 0 ;
		cv = new ContentValues();
		cv.put("id_quizz", "1");
		cv.put("note", "0");
		db.insert("quizz_result", "title", cv);			
		
		
	}

	/*
	 * Modifier
	 * en prametre on ne connaite que le numero et id_quizz 
	 * il faut chercher le quizz result de meme id et d'ordre juste au dessus + user
	 */ 	
	public void unlockQuizz(int idQuizz) {
		Log.i("unlockQuizz", "DEBUT unlockQuizz");
		ContentValues cv = new ContentValues();
		cv.put("id_quizz", idQuizz);
		cv.put("note", "0");
		SQLiteDatabase db = this.getWritableDatabase();
		db.insert("quizz_result", "title", cv);
		Log.i("unlockQuizz", "FIN unlockQuizz");
		//String sql = " insert into quizz_result (id_quizz , note ) values (1, 10) ;" ;
	}
	
	

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}


}
